export const ellipsis = (value: string, maxlen: number = 15) => {
  return value.length > maxlen ? value.substring(0, maxlen) + "..." : value;
};

export type ContainerImage = {
  image: string;
  version?: string;
};
export const makeContainerImage = (
  image: string,
  version?: string
): ContainerImage => {
  return {
    image,
    version
  };
};

export const parseImage = (image: string): ContainerImage => {
  let lastSplit = image.lastIndexOf(":");
  if (lastSplit === -1) {
    return makeContainerImage(image);
  }

  const imgHost = image.substring(0, lastSplit);
  const imgVersion = image.substring(lastSplit);

  return makeContainerImage(imgHost, imgVersion);
};

export const randStr = (length: number = 5) => {
  let result = "";
  const characters = "abcdefghijklmnopqrstuvwxyz0123456789";
  const charactersLength = characters.length;
  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
};
